<?php
      $host = 'localhost:3306' ;
      $user = 'root' ;
      $pwd = 'root' ;
      $dbname = 'test2212' ;
      // 连接数据库
      $con = mysqli_connect($host , $user , $pwd , $dbname) ;
  
      if(!$con) {
          die('数据库连接失败') ;    // 结束整个php语句
      }


    $res = [];


    //   需要前端携带数据  --  哪个字段进行排序   升序或者降序(1和2)
    $orderName = $_GET['orderName'] ;
    $orderType = $_GET['orderType'] ;

    // echo($orderType);

    if($orderType && $orderName) {
        if($orderType == 1) {
            $type = 'asc';
        } else {
            $type = 'desc';
        }

        $sql = "SELECT * FROM goods_list ORDER BY $orderName $type ";

        // 执行sql语句
        $a = mysqli_query($con , $sql) ;
    
        // 转对象   ---  每次只转一个
        // $arr = mysqli_fetch_array($a) ;
    
        // echo(json_encode($arr)) ;
    
    
        $list = [] ;
    
        while($arr = mysqli_fetch_array($a)) {
            array_push($list , $arr) ;
        }
    
        if($list) {
            $res['status'] = true ;
            $res['msg'] = '数据请求成功';
            // data随便写的
            $res['data'] = $list ;
        } else {
            $res['status'] = false ;
            $res['msg'] = '数据库错误';
        }
    
    } else {
       
        $res['status'] = false ;
        $res['msg'] = '前端数据有误';
    }


    echo(json_encode($res)) ;

    



